<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace app\massage\controller;

class AdminCoupon extends \app\AdminRest
{
    protected $model;
    protected $atv_model;
    protected $record_model;
    public function __construct(\think\App $app)
    {
        parent::__construct($app);
        $this->model = new \app\massage\model\Coupon();
        $this->atv_model = new \app\massage\model\CouponAtv();
        $this->record_model = new \app\massage\model\CouponRecord();
    }
    public function couponList()
    {
        $input = $this->_param;
        $dis[] = ["uniacid", "=", $this->_uniacid];
        if(!empty($input["status"])) {
            $dis[] = ["status", "=", $input["status"]];
        } else {
            $dis[] = ["status", ">", -1];
        }
        if($this->_user["is_admin"] == 0) {
            $dis[] = ["admin_id", "=", $this->_user["admin_id"]];
        }
        if(isset($input["send_type"])) {
            $dis[] = ["send_type", "=", $input["send_type"]];
        }
        if(!empty($input["name"])) {
            $dis[] = ["title", "like", "%" . $input["name"] . "%"];
        }
        $admin_model = new \app\massage\model\Admin();
        if(isset($input["admin_id"])) {
            $dis[] = ["admin_id", "=", $input["admin_id"]];
        }
        $data = $this->model->dataList($dis, $input["limit"]);
        if(!empty($data["data"])) {
            $city_model = new \app\massage\model\City();
            foreach ($data["data"] as &$v) {
                if(!empty($v["admin_id"])) {
                    $v["admin_name"] = $admin_model->where(["id" => $v["admin_id"]])->value("agent_name");
                    $city_id = $admin_model->where(["id" => $v["admin_id"]])->value("city_id");
                    $v["admin_city"] = $city_model->where(["id" => $city_id])->value("title");
                } else {
                    $v["admin_name"] = "平台";
                }
            }
        }
        return $this->success($data);
    }
    public function couponAdd()
    {
        $input = $this->_input;
        $input["uniacid"] = $this->_uniacid;
        if($this->_user["is_admin"] == 0) {
            $input["admin_id"] = $this->_user["admin_id"];
        }
        $res = $this->model->dataAdd($input);
        return $this->success($res, 200, $res);
    }
    public function couponUpdate()
    {
        $input = $this->_input;
        $dis = ["id" => $input["id"]];
        $input["uniacid"] = $this->_uniacid;
        if(isset($input["status"]) && $input["status"] == -1) {
            $atv_record_model = new \app\massage\model\CouponAtvRecord();
            $have_atv = $atv_record_model->couponIsAtv($input["id"]);
            if($have_atv) {
                $this->errorMsg("该优惠券正在参加活动，只有等用户发起等活动结束后才能删除");
            }
        }
        $res = $this->model->dataUpdate($dis, $input);
        return $this->success($res);
    }
    public function couponInfo()
    {
        $input = $this->_param;
        $dis = ["id" => $input["id"]];
        $res = $this->model->dataInfo($dis);
        return $this->success($res);
    }
    public function couponAtvInfo()
    {
        $input = $this->_param;
        $dis = ["uniacid" => $this->_uniacid];
        $data = $this->atv_model->dataInfo($dis);
        return $this->success($data);
    }
    public function couponAtvUpdate()
    {
        $input = $this->_input;
        $dis = ["uniacid" => $this->_uniacid];
        $input["uniacid"] = $this->_uniacid;
        $data = $this->atv_model->dataUpdate($dis, $input);
        return $this->success($data);
    }
    public function couponRecordAdd()
    {
        $input = $this->_input;
        foreach ($input["user"] as $value) {
            $res = $this->record_model->recordAdd($input["coupon_id"], $value["id"], $value["num"]);
            if(!empty($res["code"])) {
                $this->errorMsg($res["msg"]);
            }
        }
        $user = array_column($input["user"], "id");
        $coupon = $this->model->dataInfo(["id" => $input["coupon_id"]]);
        publisher(json_encode(["user" => $user, "coupon" => $coupon, "action" => "coupon_notice"], true));
        return $this->success($res);
    }
    public function couponData()
    {
        $input = $this->_param;
        $limit = $input["limit"];
        $page = $input["page"];
        $start = $limit;
        $end = ($page - 1) * $limit;
        $dis_sql = "a.uniacid = " . $this->_uniacid;
        $where[] = ["uniacid", "=", $this->_uniacid];
        if(!empty($input["title"])) {
            $title = $input["title"];
            $dis_sql .= " AND a.title like '%" . $title . "%'";
            $where[] = ["title", "like", "%" . $title . "%"];
        }
        if(isset($input["admin_id"])) {
            $admin_id = $input["admin_id"];
            $dis_sql .= " AND a.admin_id = " . $admin_id;
            $where[] = ["admin_id", "=", $admin_id];
        }
        if($this->_user["is_admin"] == 0) {
            $admin_id = $this->_user["admin_id"];
            $dis_sql .= " AND a.admin_id = " . $admin_id;
            $where[] = ["admin_id", "=", $admin_id];
        }
        $input["top_type"] = !empty($input["top_type"]) ? $input["top_type"] : 0;
        $sort = !empty($input["sort"]) && $input["sort"] == 2 ? "asc" : "desc";
        switch ($input["top_type"]) {
            case 1:
                $top = "send_type_desc " . $sort . ",total_num " . $sort . ",a.id desc";
                break;
            case 2:
                $top = "get_num " . $sort . ",a.id desc";
                break;
            case 3:
                $top = "use_num " . $sort . ",a.id desc";
                break;
            case 4:
                $top = "discount_price " . $sort . ",a.id desc";
                break;
            case 5:
                $top = "total_price " . $sort . ",a.id desc";
                break;
            case 6:
                $top = "total_balance " . $sort . ",a.id desc";
                break;
            case 7:
                $top = "order_count " . $sort . ",a.id desc";
                break;
            case 8:
                $top = "old_user_num " . $sort . ",a.id desc";
                break;
            case 9:
                $top = "new_user_num " . $sort . ",a.id desc";
                break;
            default:
                $top = "a.id desc";
        }
        $coupon_model = new \app\massage\model\Coupon();
        $coupon_record = new \app\massage\model\CouponRecord();
        $list = $coupon_record->alias("a")->join("massage_service_user_list b", "a.user_id = b.id")->where(["a.status" => 2, "a.is_new" => 2])->whereRaw("DATE_FORMAT(FROM_UNIXTIME(a.use_time),'%m/%d/%Y') = DATE_FORMAT(FROM_UNIXTIME(b.create_time),'%m/%d/%Y')")->group("a.id")->column("a.id");
        $coupon_record->where("id", "in", $list)->update(["is_new" => 1]);
        $count = $coupon_model->where($where)->count();
        $sql = "SELECT if(a.send_type=2,0,1) as send_type_desc,a.send_type,a.id,b.coupon_id as record_id,b.id as order_id,a.admin_id,h.agent_name,a.title,a.status,a.stock+ifnull(b.total_num,0) as total_num,ifnull(b.new_user_num,0) as new_user_num,ifnull(b.old_user_num,0) as old_user_num,ifnull(b.total_num,0) as get_num,ifnull(order_count,0) as order_count,ifnull(b.use_num,0) as use_num,round(ifnull(discount_price,0),2) as discount_price,round(ifnull(total_price,0),2) as total_price,round(ifnull(discount_price/total_price*100,0),2) as total_balance\n             FROM `ims_massage_service_coupon` `a`\n             LEFT JOIN (SELECT sum(num) as total_num,sum(CASE WHEN status = 2 THEN num ELSE 0 END ) as use_num,sum(CASE WHEN status = 2 AND is_new = 2 THEN num ELSE 0 END ) as old_user_num,sum(CASE WHEN status = 2 AND is_new = 1 THEN num ELSE 0 END ) as new_user_num,coupon_id,id FROM `ims_massage_service_coupon_record` GROUP BY coupon_id) AS b ON a.id=b.coupon_id \n             LEFT JOIN (SELECT agent_name,id FROM `ims_shequshop_school_admin`) AS h ON a.admin_id=h.id\n             LEFT JOIN (SELECT sum(aa.discount) as discount_price,sum(dd.true_service_price+dd.material_price) as total_price,count(dd.id) as order_count,aa.coupon_id FROM `ims_massage_service_order_list` dd LEFT JOIN `ims_massage_service_coupon_record` as aa ON dd.id=aa.order_id where dd.pay_type > -1 GROUP BY aa.coupon_id) as gg ON gg.coupon_id = a.id\n             WHERE " . $dis_sql . " GROUP BY a.id ORDER BY " . $top . " LIMIT " . $start . " OFFSET " . $end;
        $data = \think\facade\Db::query($sql);
        $admin_model = new \app\massage\model\Admin();
        $city_model = new \app\massage\model\City();
        if(!empty($data)) {
            foreach ($data as &$v) {
                if($v["send_type"] != 2) {
                    $v["total_num"] = "不限";
                }
                if($v["admin_id"] == 0) {
                    $v["agent_name"] = "平台";
                } else {
                    $city_id = $admin_model->where(["id" => $v["admin_id"]])->value("city_id");
                    $v["admin_city"] = $city_model->where(["id" => $city_id])->value("title");
                }
            }
        }
        $arr["data"] = $data;
        $arr["total"] = $count;
        $arr["current_page"] = $page;
        $arr["per_page"] = $limit;
        $arr["last_page"] = ceil($arr["total"] / $limit);
        return $this->success($arr);
    }
}

?>